<?php

namespace app\api\controller;

use app\api\model\group\GroupEvaluateComment;
use app\api\model\group\GroupEvaluateZan as GroupEvaluateZanModel;
use app\api\model\group\GroupProjectApply;
use app\common\controller\Api;
use app\api\model\group\GroupEvaluate as GroupEvaluateModel;
use think\Db;

class Groupevaluate extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['lists'];

    //点评排名
    public function ranking()
    {

        $city_id = input('city_id',0);

        $where = ['g.status'=>1];

        /*if ($city_id){
            $where['g.activity_city_id'] = $city_id;
        }*/

        $project = GroupProjectApply::alias('pa')
            ->join('group g','pa.group_id = g.group_id')
            ->where($where)
            ->field('pa.category_id,pa.is_recommend,pa.name as project_name,g.group_id,g.image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province_id,g.activity_city_id,g.activity_district_id,g.comment_num')
            ->order('g.comment_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);
    }

    //我的评价
    public function myEvaluate()
    {
        $page_size = input('page_size',10);

        $where = ['ge.user_id'=>$this->auth->id];

        $list = GroupEvaluateModel::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->join('group g','ge.group_id = g.group_id')
            ->join('group_project_apply pa','ge.group_id = pa.group_id')
            ->where($where)
            ->field('ge.evaluate_id,ge.zan_num,ge.content,ge.images,ge.createtime,u.nickname,u.avatar,
            pa.category_id,pa.is_recommend,pa.name as project_name,g.group_id,g.image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province_id,g.activity_city_id,g.activity_district_id')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $info = GroupEvaluateZanModel::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
            $list[$k]['is_zan'] = $info?1:0;
            $list[$k]['comment_num'] = GroupEvaluateComment::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);
    }

    //评价列表
    public function lists()
    {
        $page_size = input('page_size',10);
        $group_id = input('group_id',0);

        $where = ['ge.group_id'=>$group_id];

        $list = GroupEvaluateModel::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.evaluate_id,ge.zan_num,ge.content,ge.images,ge.createtime,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            if ($this->auth->id){
                $info = GroupEvaluateZanModel::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
                $list[$k]['is_zan'] = $info?1:0;
            }

            $list[$k]['comment_num'] = GroupEvaluateComment::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);
    }

    //提交评价
    public function add()
    {
        $content = input('content','');
        $images = input('images','');
        $order_id = input('order_id',0);
        $group_id = input('group_id',0);
        $video = input('video','');
        $head_score = input('head_score',0);
        $league_score = input('league_score',0);
        $activity_score = input('activity_score',0);
        $discipline_score = input('discipline_score',0);
        $style_score = input('style_score',0);
        $love_score = input('love_score',0);
        if (!$order_id&&!$group_id)$this->error('参数缺少');
        if (empty($content))$this->error('请输入评价内容');
        $info = GroupEvaluateModel::get(['user_id'=>$this->auth->id, 'order_id'=>$order_id]);
        if ($info)$this->error('已评价');
        $evaluate_model = new GroupEvaluateModel;

        $data = [
            'user_id'=>$this->auth->id,
            'order_id'=>$order_id,
            'group_id'=>$group_id,
            'content'=>$content,
            'images'=>$images,
            'video'=>$video,
            'head_score'=>$head_score,
            'league_score'=>$league_score,
            'activity_score'=>$activity_score,
            'discipline_score'=>$discipline_score,
            'style_score'=>$style_score,
            'love_score'=>$love_score,
        ];

        $res = $evaluate_model->save($data);
        if ($res){
            \app\api\model\group\Group::where(['group_id'=>$group_id])->setInc('comment_num');
            \app\api\model\group\GroupOrder::update(['is_evaluate'=>1],['order_id'=>$order_id]);
            $this->success('评价成功');
        } else {
            $this->error('评价失败');
        }

    }

    //评论评价
    public function comment()
    {
        $comment_model = new GroupEvaluateComment();

        $data = [
            'user_id'=>$this->auth->id,
            'evaluate_id'=>input('evaluate_id',0),
            'content'=>input('content',''),
        ];
        if (empty($data['evaluate_id']))$this->error('缺少参数评价id');

        if (empty($data['content']))$this->error('请输入评论内容');

        $res = $comment_model->save($data);

        $this->success('评论成功');
    }

    //评论列表
    public function commentlists()
    {
        $page_size = input('page_size',10);
        $evaluate_id = input('evaluate_id',0);

        $where = ['ge.user_id'=>$this->auth->id,'ge.evaluate_id'=>$evaluate_id];

        $list = GroupEvaluateComment::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        $this->success('',$list);

    }
}